package ca.liu.study.springdata.jpa.domain;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;

import ca.liu.study.springdata.jpa.domain.superclass.IdentityIdDomain;

@Entity
public class Course extends IdentityIdDomain<Long> {

	private String name;
	
	@ManyToMany
	private List<Student> students;

	@ManyToOne
	private Teacher teacher;

	public Teacher getTeacher() {
		return teacher;
	}

	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}

	public List<Student> getStudents() {
		return students;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public Course() {
		students = new ArrayList<Student>();
	}
	
	public Course(String name) {
		this();
		this.name = name;
	}
	
    public boolean equals(Object obj) {
    	//check id equal
    	if(!super.equals(obj)) {
    		return false;
    	}
    	
    	//check name equal
        if (obj instanceof Course) {
        	Course other = (Course) obj;
            return Objects.equals(getName(), other.getName());
        }
        return false;
    }
    
	public String toString() {
		return super.toStringHelper()
				.add("name", name)
				.add("students.size", students.size())
				.add("teacher", teacher).toString();
	}
}
